# 新写法（0.2 之后推荐）
from langchain_community.document_loaders import (
    TextLoader,
    PyPDFLoader,
    Docx2txtLoader,
    UnstructuredExcelLoader,
)
from langchain.text_splitter import CharacterTextSplitter


class ChatDoc():
    def __init__(self):
        self.doc = None
        self.splitText = []

    def getFile(self):
        doc = self.doc
        loaders = {
            "docx": Docx2txtLoader,
            "pdf": PyPDFLoader,
            "xlsx": UnstructuredExcelLoader
        }
        file_extension = doc.split(".")[-1]
        loader_class = loaders.get(file_extension)
        if loader_class:
            loader = loader_class(doc)
            text = loader.load()
            return text
        return None

    # 处理文档函数
    def splitSentences(self):
        full_text = self.getFile()
        if full_text is not None:
            text_splitter = CharacterTextSplitter(chunk_size=100, chunk_overlap=20)
            texts = text_splitter.split_documents(full_text)
            self.splitText = texts


chat_doc = ChatDoc()
chat_doc.doc = "file/若依环境使用手册.docx"
chat_doc.splitSentences()
print(chat_doc.splitText)
